(function($){

    var f = $('#form_order');
    var price=f.find('.form-fee-total').html().replace('¥','');
    var price_org=f.find('.form-fee-total-org').html().replace('¥','');

    $('label').click(function(){
        var t=$(this);
        if(t.hasClass('_dis')){
            return false;
        }
        var radio = t.find('input[type="radio"]');
            radio.attr('checked',true);
        var name=radio.attr('name');
        t.closest('form').find('input[name="'+name+'"]').closest('label').removeClass('_on');
        t.addClass('_on');
        console.log(t);
        var price_val = $('.price._on').find('input').val();
        if(price_val) {
            price = price_val.replace(/[^0-9]/ig, "");
            //console.log(price_num);
            if(price) {
                $('#all_money').text('¥' + price);
            }else{
                price=f.find('.form-fee-total').html().replace('¥','');
            }
        }
    })

    f.find('input[name=quantity]').change(function(){
        var t=$(this);
        if(t.val()>10){t.val(10);}
        var quantity=t.val()*1;
        var fee=f.find('.form-fee-total');
        fee.html('¥'+(price*quantity)+'.00');

        f.find('.form-fee-total-org').html('¥'+(price_org*quantity)+'.00');

        var dis =  (price_org*quantity) - (price*quantity);
        f.find('.form-fee-total-save').html(dis+'.00');

    })

    var $quantity = f.find('input[name=quantity]');
    $quantity.siblings('.btn-add').click(function(){
        var a = $(this);
        var val = $quantity.val();
        if(val<10){val++;}
        $quantity.val(val);
        f.find('input[name=quantity]').trigger('change');
    })

    $quantity.siblings('.btn-reduce').click(function(){
        var a = $(this);
        var val = $quantity.val();
        if(val>1){val--;}
        $quantity.val(val);
        f.find('input[name=quantity]').trigger('change');
    })


    $('input[type="tel"]').each(function(){
        var t=$(this);
        t.attr('onafterpaste',"this.value=this.value.replace(/\D/g,'')");
        t.attr('pattern',"[0-9]");
    })

    $('input[type="tel"]').keyup(function(){
        var t=$(this);
        t.val(t.val().replace(/\D/g,''));
    })


    $('._btn_pop_kf').click(function(){
        PlayJsAdPopWin();
    })

    $('._btn_sms').each(function(){
        if(G.uat!=''){
            $(this).attr("href","sms:18512196120");
        }else{
            $(this).attr("href","javascript:alert('短信下单请发送：\\n--------------------------------------\\n收货地址 + 收货人 + 收货电话 \\n到：185 121 96120 \\n--------------------------------------\\n我们收到短信后会与您联系确认 ');");
        }
        
    })

    $('._btn_tel').each(function(){
        if(G.uat!=''){
            $(this).attr("href","tel:4001685673");
        }else{
            $(this).attr("href","javascript:alert('电话订购请拨打免费热线：\\n--------------------------------------\\n 400 168 5673 \\n--------------------------------------\\n');");
        }
        
    })


    // console.log(G.af);
    //&ip=110.85.80.246 //&ip=59.58.159.176
    //$.getScript('http://int.dpool.sina.com.cn/iplookup/iplookup.php?'+(G.debug?'ip=59.58.159.176&':'')+'format=js',function(){
    //    if(G.debug){
    //        // console.log(remote_ip_info.city);
    //    }
    //    if(typeof remote_ip_info != 'undefined'){
    //        if(typeof remote_ip_info.city != 'undefined'){
    //            if(remote_ip_info.city.indexOf('\u8386\u7530')>=0&&!G.af){
    //                if(G.debug){
    //                    // console.log('err');
    //                    // return false;
    //                }
    //                window.location.href='err.php?utm_source='+encodeURI(G.url);
    //            }
    //        }
    //    }
    //
    //});


    //
    var pt=new $.Traker();
    pt.a();

    //

    $(window).scroll(function(){
        fixed_footer_adjust();
    });
    fixed_footer_adjust();

    $('._btn_scroll_buy').click(function(){
        $("html,body").animate({scrollTop:$("#a_order").offset().top},300);
    })

    $('._btn_top').click(function(){
        $("html,body").animate({scrollTop:0},300);
    })

    $('._btn_scroll_more').click(function(){
        $("html,body").animate({scrollTop:$("#QGMS").offset().top},300);
    })

    // console.log(G);
    if(G.url.indexOf('item.php')>=0){
        $('._btn_scroll_more').show();
        $('._btn_scroll_buy').show();
    }


    if(G.uat!=''){
        //修复ios的focus时的问题
        $('input,textarea').focus(function(){
            $('.fixed-footer').css({position:'absolute','bottom':'auto'}).addClass('_ios-fixed-debug');
            fixed_footer_adjust();
        })
        $('input,textarea').blur(function(){
            $('.fixed-footer').css({position:'fixed','top':'auto','bottom':'0px'}).removeClass('_ios-fixed-debug');
            fixed_footer_adjust();
            
        })
    }

})(jQuery);


//
//function PlayJsAdPopWin(){
//    if(G.uat!=''){
//        window.open('http://wpa.qq.com/msgrd?v=3&uin=2733490360&site=qq&menu=yes','_blank');
//    }else{
//        popwin=window.location.href="tencent://message/?Menu=yes&amp;uin=800132181&amp;Service=58&amp;SigT=A7F6FEA02730C988457765F40057A1C323B9BF76ECBA2719B4DDEDA76665E40BA1CA7B2B59399BDDA4FDB4752DEB04FA4B372E5C764A95C3F8C3CDD2BEEB5ECA2A4BE91D8D86FA5DD8FFBDB4FCB0392B53D8EFC01A989561E1D9A791978743F8B4D940AB411EE96AC27FBFFAC5D1C0121D385B6BEDEB419B&amp;SigU=30E5D5233A443AB2265A4C6D73160EC324110095DE67623EE68A5FFD27D5B4038A31373852B8DD667940ED78DA844086F41697C6092E423EA2DD4A04106916762B989FA13DC123DC";
//        //popwin=window.location.href="tencent://AddContact/?fromId=45&fromSubId=1&subcmd=all&uin=800132181";
//
//    }
//
// };
// setTimeout("PlayJsAdPopWin()",1000);




//调整底部固定条的显示与否
function fixed_footer_adjust(){
    var w = $(window).scrollTop();
    var wh = $(window).height();

    if($("#a_order").length){
        var o = $("#a_order").offset().top;
        var oh = $("#form_order").height();

        var s = w-(o-wh);
        // console.log('s='+s);
        // console.log('o='+o);

        if(s>200&&s<(oh+80)){
            $('.fixed-footer').slideUp();
        }else{
            $('.fixed-footer').slideDown();
        }
    }


    if(w>0){
        $('._btn_top').fadeIn();
    }else{
        $('._btn_top').fadeOut();
    }

    if(G.uat!=''){
        $('._ios-fixed-debug').css({'top':((w+wh-$('._ios-fixed-debug').outerHeight())+'px')});
    }


}

